
import javax.servlet.ServletException;
//comment
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.util.Enumeration;
import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;


@SuppressWarnings("serial")
public class HttpRequestServlet extends HttpServlet {
    private static String htmlHead = "<html><head></head><body>";
    private static String htmlTail = "</body></html>";

    private void fieldInfo(
        PrintWriter o, String name, String info )
    {
        o.print( "<b>" + name + "</b>:" );
        o.print( info );
        o.print( "<br>" );
    }

    private void dumpInfo(
        PrintWriter o, HttpServletRequest request )
    {
        o.print("<h2>url</h2>");
        o.print("<p>");
        fieldInfo(o, "method", request.getMethod() );
        fieldInfo(o, "servlet-path", request.getServletPath() );
        fieldInfo(o, "path-info", request.getPathInfo() );
        fieldInfo(o, "query", request.getQueryString() );
        o.print("</p>");
        o.print("<h2>headers</h2>");
        o.print("<p>");
        Enumeration names = request.getHeaderNames();
        while ( names.hasMoreElements() ) {
            String name = names.nextElement().toString();
            fieldInfo(o, name, request.getHeader(name) );
        }
        o.print("</p>");
    }

    private void dumpQuery( PrintWriter o, String query ) {
        o.print("<h2>query</h2>");
        o.print("<p>");
        String[] pairs = query.split("\\&");
        for ( int i=0; i < pairs.length; i++) {
            String[] fields = pairs[i].split("=");
            try {
                String n =
                    URLDecoder.decode(fields[0], "ISO-8859-1");
                String v =
                    URLDecoder.decode(fields[1], "ISO-8859-1");
                fieldInfo( o, n, v );
            }
            catch( UnsupportedEncodingException ex ) {
                // ignore
            }
        }
        o.print("</p>");
    }

    protected void doGet(
        HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println( htmlHead );
        dumpInfo( out, request );
        String q = request.getQueryString();
        if ( q != null ) {
            dumpQuery(out, q );
        }
        out.println( htmlTail );
    }

    protected void doPost(
        HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println( htmlHead );
        dumpInfo( out, request );
        BufferedReader rd = request.getReader();
        out.println("<h2>content</h2>");
        int ch;
        StringBuilder sb = new StringBuilder();
        while ( (ch=rd.read()) != -1 ) {
            sb.append( (char)ch);
        }
        dumpQuery(out, sb.toString() );
        out.println( htmlTail );
    }
}
